package battleroom;

import com.jme3.bounding.BoundingBox;
import com.jme3.bounding.BoundingSphere;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.PhysicsCollisionEvent;
import com.jme3.bullet.collision.PhysicsCollisionListener;
import com.jme3.bullet.collision.PhysicsCollisionObject;
import com.jme3.bullet.collision.shapes.BoxCollisionShape;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.SphereCollisionShape;
import com.jme3.bullet.control.GhostControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.math.Matrix3f;
import com.jme3.math.Matrix4f;
import com.jme3.math.Plane;
import com.jme3.math.Quaternion;
import com.jme3.math.Ray;
import com.jme3.math.Transform;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.shape.Line;
import com.jme3.scene.shape.Sphere;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author Adam & Zach
 * 
 */

public class PlayerGhost extends GhostControl
    implements  PhysicsTickListener, PhysicsCollisionListener
{
    public static final float detectionDist = PlayerState.PlayerHeight*100;
    protected PlayerState playerState;
    private List<StarControl> intersecting;
    private int previousIntersections = 0;
    
    
    public PlayerGhost(CollisionShape shape, PlayerState playerState)
    {
        super(shape);
        this.playerState = playerState;
        
        intersecting = new ArrayList<StarControl>();
        
        this.setCollisionGroup(PhysicsCollisionObject.COLLISION_GROUP_02);
        this.setCollideWithGroups(PhysicsCollisionObject.COLLISION_GROUP_03);
    }
    
    void attachToWorld(Node root, PhysicsSpace physicsSpace) {         
        root.addControl(this);
        physicsSpace.add(this);
        physicsSpace.addTickListener(this);
        physicsSpace.addCollisionListener(this);
    }
    
    @Override
    public void update(float tpf)
    {
        super.update(tpf);
    }
    
    @Override
    public void collision(PhysicsCollisionEvent event) {
        StarControl sc = event.getNodeB().getControl(StarControl.class);
        if(sc!= null)
        {
            if(!intersecting.contains(sc))
                intersecting.add(sc);
        }
    }
    
    @Override
    public void prePhysicsTick(PhysicsSpace space, float f) {
        //Nothing
    }
    
    
    @Override
    public void physicsTick(PhysicsSpace space, float f) {
        if(this.getOverlappingCount() > 0)
        {
            isColliding = true;
        }else{
            isColliding = false;
        }
        
        if(previousIntersections != this.getOverlappingCount())
        {
            updateIntersectList();
            previousIntersections = this.getOverlappingCount();
        }
        
    }
    
    public StarControl getClosestStar()
    {
        if(!isColliding()) return null;
        
        StarControl closest = null;        
        float closestDist = 0;
        for(StarControl star : intersecting )
        {
            if(closest == null)
            {
                closest = star;
                closestDist = this.getPhysicsLocation().subtract(star.getSpatial().getLocalTranslation()).length();
            }
            
            float distance = this.getPhysicsLocation().subtract(star.getSpatial().getLocalTranslation()).length();
            if(distance < closestDist)
            {
                closestDist = distance;
                closest = star;
            }
        }
        
        return closest;
    }
    
    public void updateIntersectList()
    {
        List<StarControl> toRemove = new ArrayList();
        Iterator<StarControl> i = intersecting.iterator();
        for(StarControl ctrl : intersecting)
        {
            if(this.getPhysicsLocation().subtract(ctrl.getSpatial().getLocalTranslation()).length() > detectionDist)
            {
                toRemove.add(ctrl);
            }
        }
        
        for(StarControl remove : toRemove)
        {         
            intersecting.remove(remove);
        }
    }
    
    
    private boolean isColliding = false;
    public boolean isColliding(){ return isColliding; }
    
}